package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthorityReceiveBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.ScanCardAuthResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.tools.FlowLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/22 18:30
 * @description :
 * @file : AuthorityReciveFragment.java
 */
@Route(path = UserCenterConstant.PATH_AUTHORITY_RECEIVE)
public class AuthorityReciveFragment extends BaseFragment {

	private FlowLayout mFlowLayout;
	private TextView mTv;
	private View mBtnReceive;
	private AuthorityReceiveBean mAuthorityReceiveBean;
	private CircleImageView mIvAvatar;
	private NoHttpUtils.OnResponseListener mResponseListener = new NoHttpUtils.OnResponseListener() {
		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
						String data = commonClass.getData().toString();
						ScanCardAuthResponseBean scanCardAuthResponse = JSON.parseObject(data, ScanCardAuthResponseBean.class);
						switch (scanCardAuthResponse.status) {
							case 3:
								popFragment();
								pushFragment(AuthorityReceiveNotSuccessFragment.newInstance());
								break;
							case 1: // 失败
                                popFragment();
								pushFragment(AuthorityReceiveFailFragment.newInstance(data));
								break;
							case 0: // 成功
							case 2: // 部分失败
                                popFragment();
								pushFragment(AuthorityReceiveSuccessFragment.newInstance(data));
								break;
						}
					} else {
						if (TextUtils.equals("9", commonClass.getCode())) {
							ToastUtils.showToast(commonClass.getMsg());
						} else {
							popFragment();
							pushFragment(AuthorityReceiveNotSuccessFragment.newInstance());
						}
					}
					break;
			}
		}

		@Override
		public void onFailed(String s, int i) {

		}

		@Override
		public void onFinish(int i) {
            dismissProgressDialog();
		}
	};

	public static AuthorityReciveFragment newInstance() {
		AuthorityReciveFragment fragment = new AuthorityReciveFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			String data = bundle.getString(UserCenterConstant.KEY_SCAN_AUTHORITY);
			mAuthorityReceiveBean = JSON.parseObject(data, AuthorityReceiveBean.class);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_receive_authority, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mFlowLayout = $(R.id.atl);
		mTv = $(R.id.tv);
		mBtnReceive = $(R.id.btn_receive);
		mIvAvatar = $(R.id.iv_avatar);
		if (mAuthorityReceiveBean != null) {
			ImageloaderUtils.displayImg(mAuthorityReceiveBean.faceUrl, mIvAvatar);
			List<String> authNameList = mAuthorityReceiveBean.authNames;
			mFlowLayout.setLastTagCount(authNameList.size());
			for (String authName : authNameList) {
				TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_tv_tag, mFlowLayout, false);
				textView.setText(authName);
				mFlowLayout.addView(textView);
			}
			String userName = mAuthorityReceiveBean.auUserName;
			String businessName = mAuthorityReceiveBean.businessName;
			SpannableString spannableString = new SpannableString(String.format("%s %s 要分配权限给你", businessName, userName));
			ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.common_color_c1_337cff));

			spannableString.setSpan(colorSpan, businessName.length() + 1, businessName.length() + 1 + userName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
			mTv.setText(spannableString);
//		showProgressDialog("正在接收...");
			mBtnReceive.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Map<String, Object> map = new HashMap<>();
					map.put("auUserId", mAuthorityReceiveBean.auUserId);
					map.put("businessId", mAuthorityReceiveBean.businessId);
					map.put("processId", mAuthorityReceiveBean.processId);
					showProgressDialog("正在接收...");

					NoHttpUtils.httpPost(UserCenterConstant.SCAN_CARD_AUTH, map, mResponseListener, 0);
//					popFragment();
//					pushFragment(AuthorityReceiveSuccessFragment.newInstance());
//				pushFragment(AuthorityReceiveFailFragment.newInstance());
//				pushFragment(AuthorityReceiveNotSuccessFragment.newInstance());
				}
			});
		}

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof AuthorityReciveFragment) {
			mActivity.setTitle("权限接收");
		}
	}

	@Override
	public void requestDatas() {

	}
}
